﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace DFGK_WMS.WebAPI.Controllers.AGV
{
    /// <summary>
    /// 出入库-AGV
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class OperationAGVController : ControllerBase
    {
        public readonly IAGVRepository aGVRepository;

        public OperationAGVController(IAGVRepository aGVRepository)
        {
            this.aGVRepository = aGVRepository;
        }

        /// <summary>
        /// 出入库任务
        /// </summary>
        /// <param name="sendTaskDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SendTask")]
        public async Task<ReturnTaskStateReportedAGVDto> SendTask(SendTaskDto sendTaskDto)
        {
            if (sendTaskDto.StartPoint == null)
            {
                return new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "起点不能为空！", msgTime = DateTime.Now.ToString() };
            }
            if (sendTaskDto.TaskType == null)
            {
                return new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "任务类型不能为空！", msgTime = DateTime.Now.ToString() };
            }
            if (sendTaskDto.TaskCode == null)
            {
                return new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "任务单号不能为空！", msgTime = DateTime.Now.ToString() };
            }
            if (sendTaskDto.EndPoint == null)
            {
                return new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "终点不能为空！", msgTime = DateTime.Now.ToString() };
            }
            var responseBody = aGVRepository.SendTask(sendTaskDto);
            if (responseBody == null)
            {
                return new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "请求失败！", msgTime = DateTime.Now.ToString() };
            }
            return new ReturnTaskStateReportedAGVDto { returnStatus = 0, returnInfo = "请求成功！", msgTime = DateTime.Now.ToString() };
        }

        /// <summary>
        /// 出入库任务状态上报
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("TaskAppear")]
        public IActionResult TaskAppear(TaskStateReportedAGVDto taskStateReportedAGV)
        {
            if(taskStateReportedAGV == null || taskStateReportedAGV.TaskCode == null)
            {
                return Ok(new ReturnTaskStateReportedAGVDto { returnStatus = 1,returnInfo = "任务单号不能为空！",msgTime = DateTime.Now.ToString()});
            }
            if(taskStateReportedAGV.Method != "end")
            {
                return Ok(new ReturnTaskStateReportedAGVDto { returnStatus = 1, returnInfo = "AGV未到达指定位置！", msgTime = DateTime.Now.ToString() });
            }
            return Ok(new ReturnTaskStateReportedAGVDto { returnStatus = 0, returnInfo = "任务已完成！", msgTime = DateTime.Now.ToString() });
        }
    }
}
